#coidng:utf-8
from datetime import datetime
from flask import Blueprint,render_template,request
from ..secure import login_required
from ..utils import prettify
from ..database import Article, db

bp = Blueprint('editor', __name__, url_prefix="/editor")


@bp.route('/', methods=['GET', 'POST'])
@login_required
def editor():
    #如果是post方法就返回tinymce生成html代码，否则渲染editor.html
    if request.method=='POST':
        content = request.form.get('content')
        title = request.form.get("title")
        tags = request.form.get("tags")
        author = request.form.get("author")
        now = datetime.now().strftime("%y/%m/%d %H:%M:%S")
        ar = Article(
                id=None, 
                title=title,
                content=content,
                tag=tags,
                author=author,
                publish_time = now,
                crawl_time = now,
                publish_addr= '我的记录',
                img_path=None,
                is_valid=1,
                bookmark_num=0,
                point_up_num=0
            )
        db.session.add(ar)
        db.session.commit()

        return 'add success'

    return render_template('editor.html')

